import store from '../store'
// import qs from qs
class Request {
	getData({
		url,
		method = 'GET',
		data = {}
	}) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: "http://api.dailyhunche.com" + url,
				method: method,
				header: {
					'content-type': 'application/x-www-form-urlencoded',
					'access-token': store.state.token
				},
				data: data,
				success: res => {
					if (res.data.code == 0) {
						resolve(res.data)
					} else {
						this.showError(res.data.msg)
					}
				},
				fail: err => {
					reject(err)
					this.showError()
				}
			})
		})
	}

	showError(error) {
		if (error) {
			uni.showToast({
				title: error,
				icon: "none"
			})
		} else {
			uni.showToast({
				title: "请求错误",
				icon: "none"
			})
		}
	}
}

export {
	Request
}
